package com.link.play.fit.ai.ui.main.foodDatabase

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.link.play.fit.ai.bean.BaseFoodInfoBean
import com.link.play.fit.ai.bean.FoodInfoBean
import com.link.play.fit.ai.utils.ApiResult
import com.link.play.fit.ai.utils.HttpResponse.safeApiCall
import com.link.play.fit.ai.utils.RetrofitClient
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class FoodDatabaseViewModel : ViewModel() {

    var foodInfoBean: MutableStateFlow<BaseFoodInfoBean> = MutableStateFlow(BaseFoodInfoBean())
    var job: Job? = null

    //ai 返回来的数据
    val foodInfoAi: MutableStateFlow<BaseFoodInfoBean> = MutableStateFlow(BaseFoodInfoBean())

    fun getFoodList(name: String) {
        viewModelScope.launch {
            when (val result = safeApiCall {
                RetrofitClient.apiServer.getFoodInfo(name)
            }) {
                is ApiResult.Success<*> -> {
                    foodInfoBean.update {
                        it.copy(
                            status = 200,
                            foodInfo = result.data as List<FoodInfoBean>?,
                            recentlyEatenData = null
                        )
                    }
                }

                is ApiResult.BusinessError -> {
                    foodInfoBean.update {
                        it.copy(status = result.status, msg = result.message)
                    }
                }

                is ApiResult.HttpError -> {
                    foodInfoBean.update {
                        it.copy(status = result.code, msg = result.message)
                    }
                }

                ApiResult.NetworkError -> {
                    foodInfoBean.update {
                        it.copy(status = 400, msg = "Network connection failed")
                    }
                }
            }
        }
    }

    //获取Ai食物
    fun getAiFood(name: String) {
        viewModelScope.launch {
            when (val result = safeApiCall {
                RetrofitClient.apiServer.getFoodInfoAi(name)
            }) {
                is ApiResult.Success<*> -> {
                    foodInfoAi.update {
                        it.copy(
                            status = 200,
                            foodInfo = ArrayList<FoodInfoBean>().apply {
                                add(result.data as FoodInfoBean)
                            },
                            recentlyEatenData = null
                        )
                    }
                }

                is ApiResult.BusinessError -> {
                    foodInfoAi.update {
                        it.copy(status = result.status, msg = result.message)
                    }
                }

                is ApiResult.HttpError -> {
                    foodInfoAi.update {
                        it.copy(status = result.code, msg = result.message)
                    }
                }

                ApiResult.NetworkError -> {
                    foodInfoAi.update {
                        it.copy(status = 400, msg = "Network connection failed")
                    }
                }
            }
        }
    }

}